﻿using GameMath;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class Unit : GameObject
    {
        public Unit(GamePointF position, Size size, GameVector2F vector)
            : base(position, size)
        {
            Vector = vector;

            appliedVectors = new Stack<GameVector2F>();

            DrawGraphics();
        }

        public GameVector2F Vector { get; set; }

        public Stack<GameVector2F> appliedVectors;

        public override void Update(GameTimer timer)
        {
            while (appliedVectors.Count > 0)
            {
                GameVector2F vec = appliedVectors.Pop();

                Vector += vec;
            }

            Position *= Vector;

            #region old Code
            /* 
            Rectangle targetArea = new Rectangle((int)(Target.X - Speed), (int)(Target.Y - Speed), (int) Speed, (int) Speed);
            if (! targetArea.Contains(Position.ToPoint()))
            {


                int curX = (int)Position.X;
                int curY = (int)Position.Y;

                int destX = Target.X;
                int destY = Target.Y;

                float frameSpeed = Speed * timer.DelayFactor;

                float xModifier = 1f;
                float yModifier = 1f;

                if (destX < curX)
                {
                    xModifier = -1f;
                }

                if (destY < curY)
                {
                    yModifier = -1f;
                }

                float bigX = (Target.X - Position.X);
                float bigY = (Target.Y - Position.Y);
                double lengthToTarget = Math.Sqrt(Math.Pow(bigX, 2) + Math.Pow(bigY, 2));

                double x = (frameSpeed * bigX / lengthToTarget) * xModifier;
                double y = (frameSpeed * bigY / lengthToTarget) * yModifier;

                Position = new GamePointF(Position.X + (float)x, Position.Y + (float)y);

                if (x == 0)
                {
                    Position = new GamePointF(Target.X, Position.Y);
                }
                if (y == 0)
                {
                    Position = new GamePointF(Position.X, Target.Y);
                }
            }
            else
            {
                //Destination reached!
            }
             */
            #endregion
        }

        public void ApplyVector(GameVector2F appliedVector)
        {
            appliedVectors.Push(appliedVector);
        }

        private void DrawGraphics()
        {
            Graphics g = Graphics.FromImage(Image);

            g.FillRectangle(new SolidBrush(Color.Pink), new Rectangle(new Point(0,0), Size));
        }
    }
}
